@echo off
chcp 65001 > nul
echo ========================================
echo ONVIF设备通道认证问题修复工具
echo Terminal#906-949 问题快速修复
echo ========================================
echo.

set "PROJECT_DIR=%~dp0.."
set "LOG_DIR=%PROJECT_DIR%\logs"
set "CONFIG_DIR=%PROJECT_DIR%\src\main\resources"

echo [1/6] 检查项目环境...
if not exist "%PROJECT_DIR%\pom.xml" (
    echo 错误: 未找到Maven项目文件
    pause
    exit /b 1
)
echo ✓ 项目环境正常

echo.
echo [2/6] 检查日志文件...
if not exist "%LOG_DIR%" mkdir "%LOG_DIR%"
set "TODAY=%date:~0,4%-%date:~5,2%-%date:~8,2%"
set "LOG_FILE=%LOG_DIR%\device-auth-fix-%TODAY%.log"
echo ✓ 日志目录: %LOG_DIR%

echo.
echo [3/6] 分析认证失败日志...
echo 正在搜索401认证错误...
findstr /i "401\|unauthorized\|digest.*timeout\|connection.*reset" "%LOG_DIR%\*.log" > "%LOG_FILE%" 2>nul
if %errorlevel% equ 0 (
    echo ✓ 发现认证相关错误，详情已保存到: %LOG_FILE%
    echo.
    echo 最近的认证错误:
    type "%LOG_FILE%" | findstr /n ".*" | head -10
) else (
    echo ! 未在日志中发现明显的认证错误
)

echo.
echo [4/6] 检查设备配置...
set "DEVICE_CONFIG=%CONFIG_DIR%\application-device-config.yml"
if exist "%DEVICE_CONFIG%" (
    echo ✓ 设备配置文件存在: %DEVICE_CONFIG%
    echo.
    echo 当前认证配置:
    findstr /i "preferredAuthMethods\|timeout\|useHeadForDigest" "%DEVICE_CONFIG%"
) else (
    echo ! 警告: 设备配置文件不存在
    echo 建议创建 %DEVICE_CONFIG%
)

echo.
echo [5/6] 生成修复建议...
echo.
echo ========================================
echo 修复建议 - Terminal#906-949
echo ========================================
echo.
echo 基于日志分析，建议采取以下措施:
echo.
echo 1. 认证信息验证:
echo    - 检查设备用户名密码是否正确
echo    - 确认设备ONVIF服务是否启用
echo    - 验证用户权限是否足够
echo.
echo 2. 网络连接优化:
echo    - 增加连接超时时间到10秒
echo    - 禁用连接复用避免冲突
echo    - 使用Basic认证替代Digest认证
echo.
echo 3. 设备特定处理:
echo    - 海康设备: 使用HikvisionDigestAuth
echo    - 大华设备: 启用useHeadForDigest
echo    - 其他设备: 优先Basic认证
echo.
echo 4. 配置文件优化:
echo    建议在 application-device-config.yml 中添加:
echo.
echo    device-config:
echo      default:
echo        connectionTimeout: 10000
echo        readTimeout: 10000
echo        preferredAuthMethods: ["Basic", "Digest", "None"]
echo        useHeadForDigest: false
echo.

echo [6/6] 提供快速修复选项...
echo.
echo 选择修复操作:
echo [1] 创建优化的设备配置文件
echo [2] 生成设备连接测试脚本
echo [3] 查看详细故障排查指南
echo [4] 启动应用并监控认证日志
echo [0] 退出
echo.
set /p choice=请选择操作 (0-4): 

if "%choice%"=="1" goto create_config
if "%choice%"=="2" goto create_test_script
if "%choice%"=="3" goto show_guide
if "%choice%"=="4" goto start_monitoring
if "%choice%"=="0" goto end
goto invalid_choice

:create_config
echo.
echo 创建优化的设备配置文件...
set "BACKUP_CONFIG=%DEVICE_CONFIG%.backup.%time:~0,2%%time:~3,2%%time:~6,2%"
if exist "%DEVICE_CONFIG%" (
    copy "%DEVICE_CONFIG%" "%BACKUP_CONFIG%" > nul
    echo ✓ 原配置已备份到: %BACKUP_CONFIG%
)

echo # ONVIF设备配置 - Terminal#906-949 优化版 > "%DEVICE_CONFIG%"
echo # 生成时间: %date% %time% >> "%DEVICE_CONFIG%"
echo. >> "%DEVICE_CONFIG%"
echo device-config: >> "%DEVICE_CONFIG%"
echo   default: >> "%DEVICE_CONFIG%"
echo     connectionTimeout: 10000 >> "%DEVICE_CONFIG%"
echo     readTimeout: 10000 >> "%DEVICE_CONFIG%"
echo     socketTimeout: 10000 >> "%DEVICE_CONFIG%"
echo     preferredAuthMethods: ["Basic", "Digest", "None"] >> "%DEVICE_CONFIG%"
echo     useHeadForDigest: false >> "%DEVICE_CONFIG%"
echo     maxRetries: 3 >> "%DEVICE_CONFIG%"
echo     retryDelay: 2000 >> "%DEVICE_CONFIG%"
echo. >> "%DEVICE_CONFIG%"
echo   hikvision: >> "%DEVICE_CONFIG%"
echo     connectionTimeout: 12000 >> "%DEVICE_CONFIG%"
echo     readTimeout: 12000 >> "%DEVICE_CONFIG%"
echo     preferredAuthMethods: ["HikvisionDigest", "Digest", "Basic", "None"] >> "%DEVICE_CONFIG%"
echo     useHeadForDigest: false >> "%DEVICE_CONFIG%"
echo. >> "%DEVICE_CONFIG%"
echo   dahua: >> "%DEVICE_CONFIG%"
echo     connectionTimeout: 8000 >> "%DEVICE_CONFIG%"
echo     readTimeout: 8000 >> "%DEVICE_CONFIG%"
echo     preferredAuthMethods: ["Digest", "Basic", "None"] >> "%DEVICE_CONFIG%"
echo     useHeadForDigest: true >> "%DEVICE_CONFIG%"

echo ✓ 优化配置文件已创建: %DEVICE_CONFIG%
echo 请重启应用以应用新配置
goto end

:create_test_script
echo.
echo 创建设备连接测试脚本...
set "TEST_SCRIPT=%PROJECT_DIR%\scripts\test-device-connection.bat"

echo @echo off > "%TEST_SCRIPT%"
echo chcp 65001 ^> nul >> "%TEST_SCRIPT%"
echo echo 设备连接测试工具 >> "%TEST_SCRIPT%"
echo echo ==================== >> "%TEST_SCRIPT%"
echo echo. >> "%TEST_SCRIPT%"
echo set /p device_ip=请输入设备IP地址: >> "%TEST_SCRIPT%"
echo set /p device_port=请输入设备端口 ^(默认80^): >> "%TEST_SCRIPT%"
echo if "%%device_port%%"=="" set device_port=80 >> "%TEST_SCRIPT%"
echo set /p username=请输入用户名: >> "%TEST_SCRIPT%"
echo set /p password=请输入密码: >> "%TEST_SCRIPT%"
echo echo. >> "%TEST_SCRIPT%"
echo echo 测试网络连通性... >> "%TEST_SCRIPT%"
echo ping -n 3 %%device_ip%% >> "%TEST_SCRIPT%"
echo echo. >> "%TEST_SCRIPT%"
echo echo 测试端口连通性... >> "%TEST_SCRIPT%"
echo telnet %%device_ip%% %%device_port%% >> "%TEST_SCRIPT%"
echo echo. >> "%TEST_SCRIPT%"
echo echo 测试ONVIF服务... >> "%TEST_SCRIPT%"
echo curl -u "%%username%%:%%password%%" -X POST -H "Content-Type: application/soap+xml" -d "^<?xml version=\"1.0\" encoding=\"UTF-8\"?^>^<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\"^>^<soap:Body^>^<tds:GetDeviceInformation xmlns:tds=\"http://www.onvif.org/ver10/device/wsdl\"/^>^</soap:Body^>^</soap:Envelope^>" http://%%device_ip%%:%%device_port%%/onvif/device_service >> "%TEST_SCRIPT%"
echo pause >> "%TEST_SCRIPT%"

echo ✓ 测试脚本已创建: %TEST_SCRIPT%
echo 运行该脚本可以测试设备连接
goto end

:show_guide
echo.
echo 打开详细故障排查指南...
set "GUIDE_FILE=%PROJECT_DIR%\DEVICE_CHANNEL_AUTH_TROUBLESHOOTING.md"
if exist "%GUIDE_FILE%" (
    start "" "%GUIDE_FILE%"
    echo ✓ 故障排查指南已打开
) else (
    echo ! 故障排查指南文件不存在: %GUIDE_FILE%
)
goto end

:start_monitoring
echo.
echo 启动应用并监控认证日志...
echo 注意: 这将启动应用并实时显示认证相关日志
echo.
set /p confirm=确认启动? (y/N): 
if /i not "%confirm%"=="y" goto end

echo.
echo 启动应用...
cd /d "%PROJECT_DIR%"
start "ONVIF Manager" cmd /c "mvn spring-boot:run -Dspring-boot.run.jvmArguments=\"-Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8\""

echo 等待应用启动...
timeout /t 10 /nobreak > nul

echo.
echo 开始监控认证日志 (按Ctrl+C停止)...
echo ========================================
tail -f "%LOG_DIR%\application.log" | findstr /i "auth\|401\|digest\|channel"
goto end

:invalid_choice
echo.
echo 无效选择，请重新运行脚本
goto end

:end
echo.
echo ========================================
echo 修复工具执行完成
echo.
echo 如需进一步帮助，请查看:
echo - 故障排查指南: DEVICE_CHANNEL_AUTH_TROUBLESHOOTING.md
echo - 编码配置说明: ENCODING_SETUP.md
echo - 优化说明文档: ONVIF_OPTIMIZATION_README.md
echo ========================================
echo.
pause